import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class CinemaList extends StatefulWidget {
  @override
  _CinemaListState createState() => _CinemaListState();
}

class _CinemaListState extends State<CinemaList> {
  List cinemaLi = [];

  void getCinemaList() async {
    try {
      Dio d = Dio();
      Response r = await d.get("https://getman.cn/mock/zq/cinema");
      setState(() {
        Map resData = jsonDecode(r.toString());
        this.cinemaLi = resData["cinemas"];
      });
    } catch (e) {}
  }

  @override
  void initState() {
    super.initState();
    getCinemaList();
  }

  //存储当前页面需要的三种颜色
  Color color0 = Color.fromARGB(255, 240, 61, 55);
  Color color1 = Color.fromARGB(255, 255, 153, 0);
  Color color2 = Color.fromARGB(255, 88, 157, 175);

  List<Widget> tagQidgets(tag) {
    List<Widget> tagList = [];
    if (tag["snack"] == 1) {
      tagList.add(CinemaTag(text: "小吃", color: color1));
    }
    if (tag["allowRefund"] == 1) {
      tagList.add(CinemaTag(text: "退", color: color2));
    }
    if (tag["endorse"] == 1) {
      tagList.add(CinemaTag(text: "改签", color: color2));
    }
    if (tag["vipTag"] is String && tag["vipTag"].length > 0) {
      tagList.add(CinemaTag(text: tag["vipTag"], color: color1));
    }
    tag["hallType"].forEach((str) {
      tagList.add(CinemaTag(text: str, color: color2));
    });

    return tagList;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
        itemCount: cinemaLi.length,
        itemBuilder: (BuildContext context, int index) {
          Map _item = cinemaLi[index];
          return Container(
            padding: EdgeInsets.all(10),
            child: Column(
              children: <Widget>[
                Container(),
                //名称价格
                Padding(
                  padding: EdgeInsets.only(bottom: 3),
                  child: Row(
                    //三行文字底部对齐
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: <Widget>[
                      Expanded(
                        child: Text(
                          '${_item["nm"]}',
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                          style: TextStyle(fontSize: 16),
                        ),
                      ),
                      Text('${_item["sellPrice"]}',
                          style: TextStyle(color: color0)),
                      Text(
                        '元起',
                        style: TextStyle(color: color0),
                      )
                    ],
                  ),
                ),
                //地址距离
                Padding(
                  padding: EdgeInsets.only(bottom: 3),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: Text(
                          '${_item["addr"]}',
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                      ),
                      Text('${_item["distance"]}'),
                    ],
                  ),
                ),
                //tag标签
                Row(
                  children: tagQidgets(_item["tag"]),
                ),
                //icon和优惠信息
                Row(
                  children: <Widget>[
                    //图标
                    Container(
                      width: 17,
                      height: 17,
                      margin: EdgeInsets.only(right: 5),
                      child: Image(
                          fit: BoxFit.cover,
                          image: AssetImage('images/ka.png')),
                    ),
                    //优惠信息
                    Text(
                      '${_item["promotion"]["cardPromotionTag"]}',
                      style: TextStyle(
                          color: Color.fromARGB(255, 153, 153, 153),
                          fontSize: 12),
                    )
                  ],
                )
              ],
            ),
          );
        },
      ),
    );
  }
}

class CinemaTag extends StatelessWidget {
  CinemaTag({Key key, @required this.text, @required this.color})
      : super(key: key);
  final String text;
  final Color color;
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(right: 5),
      padding: EdgeInsets.fromLTRB(2, 0, 2, 1),
      decoration: BoxDecoration(
          //边框
          border: Border.all(color: color),
          //圆角
          borderRadius: BorderRadius.all(Radius.circular(2))),
      child: Text(
        text,
        style: TextStyle(color: color, fontSize: 11),
      ),
    );
  }
}
